package iosr11.gbase.dao;

import iosr11.data.Event;
import iosr11.data.EventSeries;
import iosr11.data.GoogleTypes;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseAttributesExtension;
import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.api.gbase.client.GoogleBaseFeed;
import com.google.api.gbase.client.GoogleBaseQuery;
import com.google.gdata.data.PlainTextConstruct;

public class EventDAOImpl extends GenericDAOImpl<Event> implements EventDAO {

	private static String TYPE = "System evet";
	private static String N_START_DATE = "startdate";
	private static String N_END_DATE = "enddate";
	private static String PLACE_NAME = "place";
	private static String SERIES_REF = "event series";
	private static String N_DEADLINE = "is deadline";
	private static String N_DESCRIPTION = "sys description";
	private Logger log = Logger.getLogger("service-log");
	
	@Override
	public List<Event> getEventForSeries(EventSeries entity) {
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault()
				.getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance()
		.getGoogleBaseQueryResult(query, SERIES_REF,
				entity.getId());
		if (feed == null) {
			log.info("brak rekordow do EventSerii o id: "+entity.getId());
			return null;
		}
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.info("brak eventow odnoszacych sie do EventSerii o id: "+entity.getId());
			return null;
		}
		List<Event> result = new ArrayList<Event>();
		for (GoogleBaseEntry entry : feed.getEntries()) {
			Event tmp = this.rewrite(entry);
			tmp.setSeries(entity);
			tmp.setId(entry.getId());
			result.add(tmp);
		}
		log.info("znaleziono "+result.size()+" eventow dla event serii o id: "+entity.getId());
		return result;
	}

	@Override
	public boolean checkType(GoogleBaseEntry entry) {
		return entry.getGoogleBaseAttributes().getItemType().equals(TYPE);
	}

	@Override
	public Class<Event> getEntityClass() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Event rewrite(GoogleBaseEntry entry) {
		Event event = new Event();
		GoogleBaseAttributesExtension ext =entry.getGoogleBaseAttributes();
		event.setId(entry.getId());
		event.setStartDate(GoogleTypes.changeGDataToCalendar(ext.getDateTimeAttribute(N_START_DATE)));
		event.setEndDate(GoogleTypes.changeGDataToCalendar(ext.getDateTimeAttribute(N_END_DATE)));
		event.setPlace(ext.getTextAttribute(PLACE_NAME));
		if (ext.getTextAttribute(SERIES_REF).contains("null")){
			log.info("brak serii dla eventu");
			event.setSeries(null);
		}
		else {
			EventSeries series = new EventSeries();
			series.setId(ext.getTextAttribute(SERIES_REF));
			event.setSeries(series);
		}
		event.setDeadline(ext.getBooleanAttribute(N_DEADLINE));
		event.setDescription(ext.getTextAttribute(N_DESCRIPTION));
		return event;
	}

	@Override
	public GoogleBaseEntry rewrite(Event entity) {
		GoogleBaseEntry entry = new GoogleBaseEntry();
		entry.setTitle(new PlainTextConstruct(TYPE));
		entry.setDraft(true);
		GoogleBaseAttributesExtension ext = entry.getGoogleBaseAttributes();
		ext.setItemType(TYPE);
		ext.addDateTimeAttribute(N_START_DATE, GoogleTypes.changeCalendarToGData(entity.getStartDate()));
		ext.addDateTimeAttribute(N_END_DATE, GoogleTypes.changeCalendarToGData(entity.getEndDate()));
		ext.addTextAttribute(PLACE_NAME, entity.getPlace());
		if (entity.getSeries()!=null){
			ext.addTextAttribute(SERIES_REF, entity.getSeries().getId());
		}
		else{
			ext.addTextAttribute(SERIES_REF, "null");
		}
		ext.addBooleanAttribute(N_DEADLINE, entity.isDeadline());
		ext.addTextAttribute(N_DESCRIPTION, entity.getDescription());
		return entry;
	}
}
